package com.mask.sca.system.util;

import com.mask.sca.system.entity.Menu;
import com.mask.sca.system.vo.MenuTreeVO;
import java.util.*;
import java.util.stream.Collectors;

public class MenuTreeUtil {
    public static List<MenuTreeVO> buildTree(List<Menu> menuList) {
        Map<Long, MenuTreeVO> map = new HashMap<>();
        for (Menu menu : menuList) {
            MenuTreeVO vo = toVO(menu);
            map.put(vo.getId(), vo);
        }
        List<MenuTreeVO> roots = new ArrayList<>();
        for (MenuTreeVO vo : map.values()) {
            if (vo.getParentId() == null || vo.getParentId() == 0) {
                roots.add(vo);
            } else {
                MenuTreeVO parent = map.get(vo.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) parent.setChildren(new ArrayList<>());
                    parent.getChildren().add(vo);
                }
            }
        }
        return roots;
    }
    private static MenuTreeVO toVO(Menu menu) {
        MenuTreeVO vo = new MenuTreeVO();
        vo.setId(menu.getId());
        vo.setParentId(menu.getParentId());
        vo.setName(menu.getName());
        vo.setPath(menu.getPath());
        vo.setComponent(menu.getComponent());
        vo.setPerms(menu.getPerms());
        vo.setType(menu.getType());
        vo.setIcon(menu.getIcon());
        vo.setOrderNum(menu.getOrderNum());
        vo.setVisible(menu.getVisible());
        vo.setEnabled(menu.getEnabled());
        vo.setRemark(menu.getRemark());
        return vo;
    }
} 